#import "RootViewController.h"
#import "XMLAppDelegate.h"
#import "XMLParser.h"
#import "PlayView.h"
#import "PlayerView.h"

PlayView *aServerIP = nil;
@implementation PlayView

@synthesize Activity, ServerIP, servertxt;



- (IBAction)switchToPlayer
{
	if([servertxt.text length]>0){
		NSString *servertext = servertxt.text;
		NSLog(@"got to switch %@",servertext);
		PlayerView *playerView =[[PlayerView alloc]initWithNibName:@"PlayerView" bundle:[NSBundle mainBundle]];
		[self.navigationController pushViewController:playerView animated:YES];
		

	}
	else
		Status.text = @"Connection Failed";
		

}


- (IBAction)switchTosequence
{
	//set servertext in the detial view for sequence sending
	
	[Activity startAnimating];
		NSString *fail = @"Connection failed";
	ServerIP = servertxt.text;
		NSString *server = [[NSString alloc] initWithFormat:@"http://%@:41402/request.html?action=list&type=sequence",servertxt.text];
		NSURL *url = [[NSURL alloc] initWithString:server];
		NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
	
	//Initialize the delegate.
	XMLParser *parser = [[XMLParser alloc] initXMLParser];
	
	//Set delegate
	[xmlParser setDelegate:parser];
	
	//Start parsing the XML file.
	BOOL success = [xmlParser parse];
		[Activity stopAnimating];
	
	if(success){
		NSLog(@"No Errors");
		NSLog(@"server: %@",server);
			rootViewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:[NSBundle mainBundle]];
		[self.navigationController pushViewController:rootViewController animated:YES];
	}
	else
		Status.text = fail;

	[Activity stopAnimating];

}


- (void)viewDidLoad {

    [super viewDidLoad];
	aServerIP = self;
	self.title = @"Main";
}

-(BOOL)textFieldShouldReturn:(UITextField *)theTextField {
	
	[theTextField resignFirstResponder];
	return	YES;
}

@end
